package com.common.ajax.server;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.springframework.beans.BeansException;
import org.springframework.util.Assert;

import com.caits.lbs.framework.log.CommonLogFactory;

/**
 * <p>
 * RequestParser
 * </p>
 * <p>
 * 用途：请求处理器类，分多种方式处理请求
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-5-12
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-5-12 下午03:11:36</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-5-12 下午03:11:36</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class RequestParser {
	
	/** 日志记录器 */
	private static Logger log = CommonLogFactory.getLog();
	
	/** 下一级处理类 */
	private static IRequest iRequestParser = null;
	
	/**
	 * 构建错误内容 
	 * @param className
	 * @param result
	 * @param desc
	 * @return
	 */
	public static Document createError(String className, int result, String desc) {
		DOMDocument doc = new DOMDocument();
		
		Element eResponse = (Element) doc.createElement(IRequest.XML_RESPONSE);
		eResponse.addAttribute(IRequest.XML_SERVER, className);

		Element eData = (Element) doc.createElement(IRequest.XML_DATA);
		eData.addAttribute(IRequest.XML_RESULT, String.valueOf(result));
		eData.addAttribute(IRequest.XML_MSG, desc);
		Element eResult = (Element) doc.createElement("ETBase");
		eData.add(eResult);
		
		eResponse.add(eData);
		
		doc.setRootElement(eResponse);
		return doc;
	}
	/**
	 * 返回错误描述
	 * 
	 * @param errcode
	 *            - 错误代码
	 * @param msg
	 *            - 错误描述
	 * @return - xml描述串
	 */
	public static String createErrorString(String className, int result, String desc) {
		String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
		xmlStr += "<response server=\""+className+"\" result=\""+result+"\">";
		xmlStr += "<data><ETBase result=\"" + result + "\" msg=\""+desc+ "\">";
		xmlStr += "</ETBase></data>";
		xmlStr += "</response>";

		return xmlStr;
	}

	/* 处理xml方式
	 * (non-Javadoc)
	 * @see com.common.ajax.server.IRequest#request(org.jdom.Document, com.common.ajax.server.SessionMap)
	 */
	public static Document parse(Document doc, SessionMap session){
		String errorMsg = IRequest.ERROR_SUCCEED_DESC;
		int result = IRequest.ERROR_SUCCEED;
		Element e = doc.getRootElement();
		String className = e.attributeValue(IRequest.XML_SERVER);
		IRequest iRequest = null;
		try {
			iRequest = iRequestParser;
			Assert.notNull(iRequest, "请求处理类不能为空，请查询配置");
		} catch (BeansException ex) {
			errorMsg = IRequest.ERROR_CLASS_NOT_FOUND_DESC;
			result = IRequest.ERROR_CLASS_NOT_FOUND;
			log.error(errorMsg+ex.getLocalizedMessage(),ex);
			return createError(className, result, errorMsg);
		}
		Document responseDoc = null;
		try {
			responseDoc = iRequest.request(doc, session);
		} catch (SQLException e1) {
			errorMsg = IRequest.ERROR_SQL_DESC;
			result = IRequest.ERROR_SQL_ACCESS;
			log.error(errorMsg+e1.getLocalizedMessage(), e1);
			return RequestParser.createError(className, result, errorMsg);
		} catch (SecurityException e1) {
			errorMsg = IRequest.ERROR_SECURITY_DESC;
			result = IRequest.ERROR_SECURITY_ACCESS;
			log.error(errorMsg+e1.getLocalizedMessage(), e1);
			return RequestParser.createError(className, result, errorMsg);
		} catch (RuntimeException e1) {
			errorMsg = IRequest.ERROR_RUNTIME_DESC;
			result = IRequest.ERROR_RUNTIME_ACCESS;
			log.error(errorMsg+e1.getLocalizedMessage(), e1);
			return RequestParser.createError(className, result, errorMsg);
		} catch (NoSuchMethodException e1) {
			errorMsg = IRequest.ERROR_METHOD_NOT_FOUND_DESC;
			result = IRequest.ERROR_METHOD_NOT_FOUND;
			log.error(errorMsg+e1.getLocalizedMessage(), e1);
			return RequestParser.createError(className, result, errorMsg);
		} catch (IllegalAccessException e1) {
			errorMsg = IRequest.ERROR_ILLEGAL_ACCESS_DESC;
			result = IRequest.ERROR_ILLEGAL_ACCESS;
			log.error(errorMsg+e1.getLocalizedMessage(), e1);
			return RequestParser.createError(className, result, errorMsg);
		} catch (InvocationTargetException e1) {
			errorMsg = IRequest.ERROR_INVOCATIONTARGET_DESC;
			result = IRequest.ERROR_INVOCATIONTARGET_ACCESS;
			log.error(errorMsg+e1.getLocalizedMessage(), e1);
			return RequestParser.createError(className, result, errorMsg);
		}
	

		return responseDoc;
	}

	/**
	 * 设置变量 iRequestParser 的值，数据类型：IRequest
	 * @param iRequestParser the iRequestParser to set
	 */
	public void setiRequestParser(IRequest iRequestParser) {
		RequestParser.iRequestParser = iRequestParser;
	}

	/**
	 * 获取变量iRequestParser的值
	 * @return the iRequestParser 数据类型 : IRequest
	 */
	public IRequest getiRequestParser() {
		return iRequestParser;
	}

}
